package com.savvy.hibernate;

import android.os.Environment;
import android.os.StatFs;

/**
 * It is a class which encapsulate Linux df command
 * 
 * @author Bennis
 * 
 */
public class Volume {

	public long getSDCardAvailableSize() throws HibernateException {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
			long blocks = sf.getAvailableBlocks();
			long bkSize = sf.getBlockSize();
			return (blocks * bkSize);
		} else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
			throw new HibernateException("Read only", HibernateException.EXINVALID);
		} else if (state.equals(Environment.MEDIA_CHECKING)) {
			throw new HibernateException("Is checking", HibernateException.EXAGAIN);
		} else if (state.equals(Environment.MEDIA_NOFS)) {
			throw new HibernateException("Need format SD card or unrecognizable file system", HibernateException.EXNOSUPPORT);
		} else if (state.equals(Environment.MEDIA_SHARED)) {
			throw new HibernateException("USB mass storage", HibernateException.EXINVALID);
		} else {
			throw new HibernateException("No SD card", HibernateException.EXUNAVAILABLE);
		}
	}

}
